package com.shopping.huaju.commodity.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shopping.huaju.commodity.entity.AttrAttrgroupRelationEntity;
import com.shopping.huaju.commodity.service.AttrAttrgroupRelationService;
import com.shopping.huaju.commodity.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.shopping.huaju.commodity.entity.AttrEntity;
import com.shopping.huaju.commodity.service.AttrService;
import com.shopping.huaju.common.utils.PageUtils;
import com.shopping.huaju.common.utils.R;

import javax.annotation.Resource;


/**
 * 商品属性表
 *
 * @author tofu
 * @email tofu@qq.com
 * @date 2023-11-07 18:28:20
 */
@RestController
@RequestMapping("commodity/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Resource
    private CategoryService categoryService;
    @Resource
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("commodity:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/list/base/{categoryId}")
    //@RequiresPermissions("commodity:attr:list")
    public R listBaseAttr(@RequestParam Map<String, Object> params, @PathVariable Long categoryId){
        //PageUtils page = attrService.queryPage(params);
        PageUtils page = attrService.queryPageBaseAttr(params, categoryId);
        return R.ok().put("page", page);
    }

    @RequestMapping("/list/sale/{categoryId}")
    //@RequiresPermissions("commodity:attr:list")
    public R listSaleAttr(@RequestParam Map<String, Object> params, @PathVariable Long categoryId){
        PageUtils page = attrService.queryPageSaleAttr(params, categoryId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("commodity:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		AttrEntity attr = attrService.getById(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 返回更加详细的属性信息--包括其对应的类别的层级id信息 和 对应的属性组
     * @param attrId
     * @return
     */
    @RequestMapping("/infoDetail/{attrId}")
    //@RequiresPermissions("commodity:attr:info")
    public R infoDetail(@PathVariable("attrId") Long attrId){
        AttrEntity attr = attrService.getById(attrId);
        Long[] cascaderCategoryId = categoryService.getCascaderCategoryId(attr.getCategoryId());
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = attrAttrgroupRelationService.list(
                new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attr.getAttrId()));
        Long attrGroupId = null;
        if(!attrAttrgroupRelationEntities.isEmpty()){
            attrGroupId = attrAttrgroupRelationEntities.get(0).getAttrGroupId();
        }
        System.out.println(attrGroupId);
        return R.ok().put("attr", attr).put("cascaderCategoryId",cascaderCategoryId).put("attrGroupId",attrGroupId);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("commodity:attr:save")
    public R save(@RequestBody AttrEntity attr){
        //需要对属性对象分类：如果是销售属性，则直接save入库
        //如果是基本属性，需要保存基本属性和属性的关联关系，需要更新两张表
		//attrService.save(attr);
        //使用自定义方法实现将区分基本属性和销售属性，将销售属性入属性库，将基本属性入属性库，同时还要记录属性和属性组的关联关系到关系库
        attrService.saveAttrAndAttrGroupRelation(attr);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("commodity:attr:update")
    public R update(@RequestBody AttrEntity attr){
        //对于基本属性，修改则需要修改两张表
		//attrService.updateById(attr);
        //使用自定义 修改方法
        attrService.updateAttrAndAttrGroupRelation(attr);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("commodity:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
